package com.codefish.codefish_blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codefish.codefish_blog.entity.ArticleLikeRecord;
import com.codefish.codefish_blog.mapper.ArticleLikeRecordMapper;
import com.codefish.codefish_blog.service.ArticleLikeRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codefish.codefish_blog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author codefish
 * @since 2022-03-22
 */
@Service
public class ArticleLikeRecordServiceImpl extends ServiceImpl<ArticleLikeRecordMapper, ArticleLikeRecord> implements ArticleLikeRecordService {

    @Override
    public void addLikeRecord(long articleId, int likerId) {
        ArticleLikeRecord record = new ArticleLikeRecord();
        record.setArticleId(articleId);
        record.setLikerId(likerId);
        this.save(record);
    }

    @Override
    public void removeLikeRecord(long articleId, int likerId) {
        QueryWrapper<ArticleLikeRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("article_id", articleId)
                .eq("liker_id", likerId);
        this.remove(wrapper);
    }

    @Override
    public boolean isLike(long articleId, int likerId) {
        QueryWrapper<ArticleLikeRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("article_id", articleId)
                .eq("liker_id", likerId);
        ArticleLikeRecord record = this.getOne(wrapper);
        return record != null;
    }
}
